﻿using System;
using System.Collections.Generic;

using RexToy.DesignPattern;

namespace RexToy.UI.WinForm.ViewModel
{
    public class ActionCommand : IUICommand
    {
        private Action _execute;
        private Func<bool> _canExecute;

        public ActionCommand(Action execute, Func<bool> canExecute = null)
        {
            execute.ThrowIfNullArgument("execute");
            _execute = execute;
            _canExecute = canExecute;
        }

        //TODO:Consider more about this property
        public virtual bool SuggestRequery
        {
            get { return true; }
        }

        public void Execute()
        {
            _execute();

            CommandBindRegistry.Instance().RequeryCanExecute();
        }

        public bool CanExecute()
        {
            if (_canExecute == null)
                return true;
            else
                return _canExecute();
        }
    }
}
